package com.javaex.book.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.javaex.book.annotation.Controller;
import com.javaex.book.annotation.RequestMapping;
import com.javaex.book.pojo.Cart;
import com.javaex.book.pojo.Order;
import com.javaex.book.pojo.OrderItem;
import com.javaex.book.pojo.User;
import com.javaex.book.service.OrderService;
import com.javaex.book.service.impl.OrderServiceImpl;
import com.javaex.book.utils.Constants;
import com.javaex.book.utils.Pages;
import com.javaex.book.utils.Pagination;
import com.javaex.book.utils.ReturnResult;
import com.javaex.book.utils.WebUtils;

@Controller("/order")
public class OrderController {

	OrderService orderService = new OrderServiceImpl();

	/**
	 * 生成订单
	 */
	@RequestMapping("/create")
	public String createOrder(HttpServletRequest req, HttpSession session) {
		// 防止表单重复提交
		if (session.getAttribute("token") == null) {
			return Pages.ORDER_CREATE;
		}
		session.removeAttribute("token");

		Cart cart = (Cart) session.getAttribute("cart");
		User loginUser = (User) session.getAttribute("loginUser");
		if (loginUser == null) {
			return Pages.redirect("/user/login");
		}
		String orderId = orderService.createOrder(cart, loginUser.getId());
		if (orderId == null || orderId.isEmpty()) {
			session.setAttribute("message", "订单结算失败");
			return Pages.ORDER_CREATE;
		}
		cart.clear();
		session.setAttribute("orderId", orderId);
		return Pages.ORDER_CREATE;
	}

	/**
	 * 我的订单
	 */
	@RequestMapping()
	public String toMyOrderManager(HttpServletRequest req, HttpSession session) {
		// 在 签收订单、删除订单 中存放的 order_message，从session转移到request中，保证其只显示一次
		String message = (String) session.getAttribute("order_message");
		if (message != null) {
			req.setAttribute("order_message", message);
			session.removeAttribute("order_message");
		}
		
		// 获取页码、页容量、登录用户信息
		Integer pageNo = WebUtils.getInteger(req, "no", Pagination.DEFAULT_PAGE_NO);
		Integer pageSize = WebUtils.getInteger(req, "size", Pagination.DEFAULT_PAGE_SIZE);
		User loginUser = (User) session.getAttribute("loginUser");
		
		// 查询我的订单总数
		Integer totalCount = orderService.countMyOrders(loginUser.getId());
		if (totalCount == 0) {
			req.setAttribute("message", "未查询到订单数据");
			return Pages.ORDER_MANAGER;
		}
		
		Pagination pagination = new Pagination(pageNo, pageSize, totalCount);
		pagination.setUrl("/order");
		req.setAttribute("page", pagination);
		
		// 查询我的订单
		List<Order> orderList = orderService.showMyOrders(loginUser.getId(), pagination.getStart(), pagination.getSize());
		req.setAttribute("orderList", orderList);
		return Pages.ORDER_MANAGER;
	}
	
	/**
	 * 签收订单
	 */
	@RequestMapping("/receiver")
	public String receiverOrder(HttpServletRequest req, HttpSession session) {
		String orderId = req.getParameter("id");
		ReturnResult result = orderService.receiverOrder(orderId);
		if (result.getCode() == Constants.Code.FAIL) {
			session.setAttribute("order_message", result.getMessage());
		}
		return Pages.redirect(req.getHeader("Referer"));
	}
	
	/**
	 * 查看订单详情
	 */
	@RequestMapping("/detail")
	public String showOrderDetail(HttpServletRequest req) {
		// 获取页码、页容量、订单id
		Integer pageNo = WebUtils.getInteger(req, "no", Pagination.DEFAULT_PAGE_NO);
		Integer pageSize = WebUtils.getInteger(req, "size", Pagination.DEFAULT_PAGE_SIZE);
		String orderId = req.getParameter("id");
		
		// 查询当前订单的订单项数量
		Integer totalCount = orderService.countOrderItems(orderId);
		if (totalCount == 0) {
			req.setAttribute("order_message", "订单 " + orderId + " 没有商品数据");
			return Pages.ORDER_DETAIL;
		}
		
		Pagination pagination = new Pagination(pageNo, pageSize, totalCount);
		pagination.setUrl("/order/detail");
		req.setAttribute("page", pagination);
		
		List<OrderItem> orderItemList = orderService.showOrderDetail(orderId, pagination.getStart(), pagination.getSize());
		req.setAttribute("orderItemList", orderItemList);
		return Pages.ORDER_DETAIL;
	}
	
	/**
	 * 删除订单
	 */
	@RequestMapping("/delete")
	public String deleteOrder(HttpServletRequest req, HttpSession session) {
		String orderId = req.getParameter("id");
		ReturnResult result = orderService.deleteOrder(orderId);
		if (result.getCode() == Constants.Code.FAIL) {
			session.setAttribute("order_message", result.getMessage());
		}
		return Pages.redirect(req.getHeader("Referer"));
	}

	/**
	 * 查看所有订单
	 */
	@RequestMapping("/all")
	public String showAllOrders(HttpServletRequest req, HttpSession session) {
		// 在 签收订单、删除订单、发货业务 中存放的 order_message，从session转移到request中，保证其只显示一次
		String message = (String) session.getAttribute("order_message");
		if (message != null) {
			req.setAttribute("order_message", message);
			session.removeAttribute("order_message");
		}
		
		// 页面标题
		req.setAttribute("title", "全部订单");
		
		// 获取页码、页容量
		Integer pageNo = WebUtils.getInteger(req, "no", Pagination.DEFAULT_PAGE_NO);
		Integer pageSize = WebUtils.getInteger(req, "size", Pagination.DEFAULT_PAGE_SIZE);
		
		// 查询我的订单总数
		Integer totalCount = orderService.countAllOrders();
		if (totalCount == 0) {
			req.setAttribute("message", "未查询到订单数据");
			return Pages.ORDER_MANAGER;
		}
		
		Pagination pagination = new Pagination(pageNo, pageSize, totalCount);
		pagination.setUrl("/order/all");
		req.setAttribute("page", pagination);
		
		// 查询订单
		List<Order> orderList = orderService.showAllOrders(pagination.getStart(), pagination.getSize());
		req.setAttribute("orderList", orderList);
		return Pages.ORDER_MANAGER;
	}

	/**
	 * 发货
	 * 	1. 根据订单id查询出订单
	 * 	2. 判断订单是否未发货，如果已发货或已签收，返回提示信息
	 * 	3. 如果未发货，将订单的状态改为已发货
	 */
	@RequestMapping("/send")
	public String sendOrder(HttpServletRequest req, HttpSession session) {
		String orderId = req.getParameter("id");
		ReturnResult result = orderService.sendOrder(orderId);
		if (result.getCode() == Constants.Code.FAIL) {
			session.setAttribute("order_message", result.getMessage());
		}
		return Pages.redirect(req.getHeader("Referer"));
	}
}